package web.banco;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import web.banco.entity.Approval;

public class DatabaseDerby implements DatabaseInterface {

	private EntityManagerFactory emf;
    private EntityManager em;
	
    
    public DatabaseDerby() {
        // Create EntityManagerFactory for persistent unit named "pu1"
        // to be used in this test
        emf = Persistence.createEntityManagerFactory("pu1");
	}
    
	@Override
	public int insertInto(APPROVAL_STATUS status, String client, String securityKey) throws Exception {

		System.out.println("Derby - insertInto(status="+status+", client="+client+", securityKey="+securityKey+")");
		
        // Persist all entities
        createTransactionalEntityManager();

        // Create new approval
    	Approval approval0 = new Approval();
        approval0.setId(1);
        approval0.setClient(client);
        approval0.setSecurityKey(securityKey);
        approval0.setStatus(status.toString());

        // Persist the approval
        em.persist(approval0);
        em.flush();
        
        closeTransactionalEntityManager();

        System.out.println("Generated Approval ID = "+approval0.getId());

		return approval0.getId();
	}

	@Override
	public APPROVAL_STATUS selectFrom(int id) throws Exception {

		System.out.println("Derby - selectFrom(id="+id+")");

        // Get a detached instance in a new EntityManager
        createEntityManager();

        Approval c = findApproval(id);
        closeEntityManager();

        APPROVAL_STATUS status = APPROVAL_STATUS.NOT_REGISTERED;
        if (c != null)
        {
        	status = APPROVAL_STATUS.valueOf(c.getStatus());
        }
        return status;
	}

	@Override
	public void update(int id, APPROVAL_STATUS status) throws Exception {

		System.out.println("Derby - update(id="+id+", status="+status+")");

        // Persist all entities
        createTransactionalEntityManager();

        Approval a = findApproval(id);

        a.setStatus(status.toString());

        System.out.println("Status before = "+a.getStatus());
        // Persist the approval
        em.merge(a);        
        em.flush();
        System.out.println("Status after = "+a.getStatus());
        
        closeTransactionalEntityManager();


	}
	
	
	 private void createTransactionalEntityManager() {

        // Create a new EntityManager
        em = emf.createEntityManager();

        // Begin transaction
        em.getTransaction().begin();
    }

    private void closeTransactionalEntityManager() {

        // Commit the transaction
        em.getTransaction().commit();

        // Close this EntityManager
        em.close();
    }

    private void createEntityManager() {

        // Create a new EntityManager
        em = emf.createEntityManager();
    }

    private void closeEntityManager() {

        // Close this EntityManager
        em.close();
    }	
    
    private Approval findApproval(int id) {

        Query q = em.createQuery("select a from Approval a where a.id = :id");
        q.setParameter("id", id);
        return (Approval)q.getSingleResult();

    }
    

}
